// pages/school/index.js
const App = getApp()
const utils = require("../../utils/util.js")
Page({

    /**
     * 页面的初始数据
     */
    data: {
        window: App.globalData.window,
        bodyBgColor: "#f0f0f0",
        notice: '各位同学请注意，所有的任务酬劳均为双方线下交易哦，任务完成后记得联系对方给予酬劳哦～',
        location_icon: '/static/img/school_location.png',
        userIcon: '/static/img/student.png',
        // 本地缓存的用户信息
        userInfo: {},
        loadMore: {
            show: false,
            type: "loading",
            text: "加载中..."
        },
        theamTextColor: App.globalData.theamTextColor,
        userConcat: {},
        userConcatShow: false,
        list: [
        ],
        page: {
            pageNum: 1,
            pageSize: 15,
            pages: 0
        }
    },
    URL: {
        pageList: '/partTimeJob/pageList'
    },
    page: {
        home: '/pages/tabbar/index/index',
        schoolPage: "/pages/school/index",
        userInfo: '/pages/tabbar/user/userInfoEdit/index',
        shareTarget: '/pages/recruit/share/index'
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        App.$handlerInviteCode(options);
        this.inInit();
    },
    getUserConcat(e) {
        this.setData({
            userConcat: utils.getEvent(e).item,
            userConcatShow: true
        })
    },
    closeUserInfo() {
        this.setData({
            userConcatShow: false
        })
    },
    makePhone(e) {
        wx.makePhoneCall({
            phoneNumber: e.currentTarget.dataset.userphone
        })
    }
    , copyWechat(e) {
        let wechat = e.currentTarget.dataset.wechat;
        wx.setClipboardData({
            data: wechat,
            success: function (res) {
                wx.getClipboardData({
                    success: function () {
                        wx.showToast({
                            title: '已复制',
                            icon: 'none'
                        })
                    }
                })
            }
        })
    },
    initGetSchoolName() {
        let schoolName = App.$getData('task_schoolName');
        let userInfo = App.$getUserInfo();
        if (utils.isInvalid(schoolName)) {
            if (!utils.isInvalid(userInfo.schoolName)) {
                schoolName = userInfo.schoolName;
            }
        }
        if (!utils.isInvalid(schoolName)) {
            App.$saveData('task_schoolName', schoolName);
        }
        return schoolName;
    },
    inInit() {
        this.initAuthInfo();
        this.setData({
            'searchParams.schoolName': this.initGetSchoolName(),
        })
        this.init(this.data.page.one, false);
    }
    // 初始化数据
    , init(pageNum, loadMore) {
        let item = this.data.searchParams;

        let params = {};
        for (const itemKey in item) {
            params[itemKey] = item[itemKey];
        }
        params['pageNum'] = pageNum
        params['pageSize'] = this.data.page.pageSize

        // 上拉到底的时候进行显示加载中
        this.setLoadingMore(loadMore, "loading", "加载中...")

        if (!loadMore ? true : loadMore) {
            // 弹出loading
            wx.showLoading({
                title: '加载中',
                mask: true,
            })
        }

        App.$get(!loadMore ? true : loadMore, this.URL.pageList, params, res => {
            wx.stopPullDownRefresh();
            this.setData({
                "loadMore.show": false,
            })
            if (res.R) {
                this.setData({
                    'page.pageNum': res.data.pageNum,
                    'page.pageSize': res.data.pageSize,
                    'page.pages': res.data.pages
                })

                let newData = res.data.rows;
                let sourceData = this.data.list;
                if (!loadMore) {
                    sourceData = newData;
                } else {
                    // 上啦刷新，列表追加数据
                    newData.forEach(data => {
                        sourceData.push(data);
                    })
                }
                this.setData({
                    list: sourceData,
                })
            }

            if (!loadMore ? true : loadMore) {
                wx.hideLoading();
            }
        })
    },

    initAuthInfo() {
        this.setData({
            'isAuth': App.$isAuth(),
            'userInfo': App.$getUserInfo(),
        })
    }
    , clearSchoolName() {
        this.setData({
            'searchParams.schoolName': '',
            'userInfo.schoolName': null,
        })
        App.$removeStore('task_schoolName');
        this.init(1, false);
    }
    // 选择学校后回调函数
    , onSchoolNameChange(e) {
        this.setData({
            'searchParams.schoolName': e.detail,
            'userInfo.schoolName': e.detail,
        })
        App.$saveData('task_schoolName', e.detail);
        this.init(1, false);
    }
    // 点击选择学校
    , reloadSchoolName() {
        App.$navigateTo(this.page.schoolPage, false)
    }
    , setLoadingMore(show, type, text) {
        this.setData({
            "loadMore.show": show,
            "loadMore.type": type,
            "loadMore.text": text,
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    goBack() {
        wx.navigateBack();
    },
    goHome() {
        App.$switchTab(this.page.home, false)
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },


    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        this.init(1, false);
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        let page = this.data.page;
        if (page.pages === 0 || page.pageNum + 1 > page.pages) {
            this.setLoadingMore(true, "end", "我是有底线的~")
            return;
        }
        this.init(page.pageNum + 1, true);
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function (e) {
        let item = null;
        if (e.from !== "menu") {
            item = e.target.dataset.detail
        }
        let str=JSON.stringify(item);
        return App.$onShare(this.page.shareTarget, "&item=" + encodeURIComponent(str), item.title);
    }
})
